/*******************************************************************************
 * Copyright (C) 2010 4Point Solutions Ltd. www.4point.com
 * 
 * Portions based on PushButton Engine, 
 * (C) 2009 PushButton Labs, LLC www.pushbuttonengine.com
 * 
 * This file is licensed under the terms of the MIT license, which is included
 * in the license.txt file at the root of this project.
 ******************************************************************************/
package com.fourpointsolutions.flex.utils.logging
{
    /**
     * Contains utility methods required by the logging classes.
     */
    public class LogUtil
    {
		/**
		 * Replaces instances of less then, greater then, ampersand, single and double quotes.
		 * @param str String to escape.
		 * @return A string that can be used in an htmlText property.
		 */		
		public static function escapeHTMLText(str:String):String
		{
			var chars:Array = 
			[
				{char:"&", repl:"|amp|"},
				{char:"<", repl:"&lt;"},
				{char:">", repl:"&gt;"},
				{char:"\'", repl:"&apos;"},
				{char:"\"", repl:"&quot;"},
				{char:"|amp|", repl:"&amp;"}
			];
			
			for(var i:int=0; i < chars.length; i++)
			{
				while(str.indexOf(chars[i].char) != -1)
				{
					str = str.replace(chars[i].char, chars[i].repl);
				}
			}
			
			return str;
		}
		
		/**
         * Keep a number between a min and a max.
         */
        public static function clamp(v:Number, min:Number = 0, max:Number = 1):Number
        {
            if(v < min) return min;
            if(v > max) return max;
            return v;
        }
    }
}